/* GLTextureManager.cpp
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#include <graphics/GLTextureManager.h>
#include <GL/gl.h>

using namespace std;

GLTextureManager* _TexManagerInstance = NULL;

GLTextureManager::GLTextureManager()
{

}

GLTextureManager::~GLTextureManager()
{
	map<unsigned char, GLTexture*>::iterator it;

	for (it = textures.begin(); it != textures.end(); it++)
		delete it->second;
}

void
GLTextureManager::setTexture(unsigned char id, GLTexture* tex)
{
	GLTexture* test = getTexture(id);

	if (test != NULL)
		delete test;

	textures[id] = tex;
}

GLTexture*
GLTextureManager::getTexture(unsigned char id)
{
	map<unsigned char, GLTexture*>::iterator it;
	it = textures.find(id);

	if (it == textures.end())
		return NULL;

	return it->second;
}

GLTextureManager&
GLTextureManager::getInstance()
{
	if (_TexManagerInstance == NULL)
		_TexManagerInstance = new GLTextureManager();

	return *_TexManagerInstance;
}


void
GLTextureManager::loadTextures()
{
	map<unsigned char, GLTexture*>::iterator it;

	for (it = textures.begin(); it != textures.end(); it++)
		it->second->load();
}

void
GLTextureManager::useTexture(unsigned char id)
{
	GLTexture* tex = getTexture(id);

	if (tex != NULL)
		tex->use();
}
